package glemipsc.implementations.glemips32;

import glemipsc.GLEMipsEmu;
import glemipsc.implementations.Argument;
import glemipsc.implementations.Instruction;

public class Jlz extends Instruction {
	
	public Jlz(Argument arg1, Argument arg2) {
		arguments.add(arg1);
		arguments.add(arg2);
	}
	
	@Override
	public String getOpCode() {
		String opcode = Instruction.JLZ;
		
		if (arguments.get(1) instanceof Register) {
			// Register
			opcode += arguments.get(0).getOpCode();
			opcode += arguments.get(1).getOpCode();
			opcode += zeros(16);
		}
		else {
			// Immediate
			opcode += arguments.get(0).getOpCode();
			opcode += zeros(5);
			opcode += arguments.get(1).getOpCode();
		}
		return opcode + "\n";
	}

	@Override
	public String getName() {
		return "JLZ";
	}

	@Override
	public void emulate(GLEMipsEmu emu) {
		Register cmp = (Register) arguments.get(0);
		if (cmp.getEmulValue() < 0) {
			if (arguments.get(1) instanceof Register)
				emu.setPC(emu.getPC() + 1 + ((Register) arguments.get(1)).getEmulValue());
			else if (arguments.get(1) instanceof Immediate)
				emu.setPC(emu.getPC() + 1 + ((Immediate) arguments.get(1)).n);
		}
	}

}
